requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {

//清除百度编译器
// if (typeof msg_content != 'undefined') {
//     msg_content.destroy();
// }
// if (typeof mail_content != 'undefined') {
//     mail_content.destroy();
// }
// if (typeof phone_content != 'undefined') {
// 	phone_content.destroy();
// }

//重新实例化
// var msg_content = UE.getEditor('msg_content');
// var mail_content = UE.getEditor('mail_content');

//初始化编辑器
    var editor1, editor2, editor3;

    var initEditor = function () {
        editor1 = window.UE.getEditor('phone_content');
        editor2 = window.UE.getEditor('msg_cont2');
        editor3 = window.UE.getEditor('letter_content');
        $('#messg-span3 span').on('click', function () {
            var placehold = $(this).data('placehold');
            editor1.focus();
            editor1.execCommand('inserthtml', placehold);//'{'+placehold+'}'
        });
        $('#messg-span2 span').on('click', function () {
            var placehold = $(this).data('placehold');
            editor2.focus();
            editor2.execCommand('inserthtml', placehold);//'{'+placehold+'}'
        });
        $('#messg-span1 span').on('click', function () {
            var placehold = $(this).data('placehold');
            editor3.focus();
            editor3.execCommand('inserthtml', placehold);//'{'+placehold+'}'
        });
        if (!getAurlparam('id')) {

        }
    }

    if (window.UE && window.UE.version) {
        initEditor();
    } else {
        setTimeout(initEditor, 200);
    }


    //初始化编辑器 end
    //var phone_content = UE.getEditor('phone_content');
    site.send({
        url: "manager/selectModeTypeList.do",
        //async:false
        data: {pageNum: 1},
        callback: function (data) {
            var list = JSON.parse(data)
            var source = $("#a_content_tpl").html();
            var template = Handlebars.compile(source);
            var html = template(list);
            $('#a_content').html(html);
            if (!getAurlparam('id')) {
                $('#msgType').removeAttr("disabled");
                $("#commitPermiss").text('确认添加')
                $('#messg-span2 span').each(function () {
                    var place = $(this).data('placehold');
                    place = place.replace('{', '{$')
                    $(this).data('placehold', place);
                })
                $('#messg-span3 span').each(function () {
                    var place = $(this).data('placehold');
                    place = place.replace('{', '{$')
                    $(this).data('placehold', place);
                })
                $('#messg-span1 span').each(function () {
                    var place = $(this).data('placehold');
                    place = place.replace('{', '{$')
                    $(this).data('placehold', place);
                })
            }

        }

    })

    if (getAurlparam('id')) {
        var sendTypes;
        var id = getAurlparam('id');

        site.send({
            url: "manager/queryModeInit.do",
            //async:false
            data: {id: id},
            callback: function (data) {
                var list = JSON.parse(data)
                $("#msgType").val(list.content.msg_types);
                $("#setSmsTitle").val(list.content.sms_title);
                $("#setMailTitle").val(list.content.mail_title);
                $("#setLetterTitle").val(list.content.msg_title);

                setTimeout(function () {//加延迟用来显示内容
                    editor2.setContent(list.content.mail_content);
                }, 500);
                setTimeout(function () {//加延迟用来显示内容
                    editor1.setContent(list.content.phone_content);
                }, 500);
                setTimeout(function () {//加延迟用来显示内容
                    editor3.setContent(list.content.msg_content);
                }, 500);

                $("#mail").hide();
                $("#mailInput").prop('checked', false);

                $("#phone").hide();
                $("#smsInput").prop('checked', false);

                $("#letter").hide();
                $("#letterInput").prop('checked', false);

                sendTypes = list.content.send_types.split(',');
                for (var i = 0; i < sendTypes.length; i++) {
                    if (sendTypes[i] == 1) {
                        $("#mail").show();
                        $("#mailInput").prop('checked', true)
                    } else if (sendTypes[i] == 2) {
                        $("#phone").show();
                        $("#smsInput").prop('checked', true)
                    } else if (sendTypes[i] == 3) {
                        $("#letter").show();
                        $("#letterInput").prop('checked', true)
                    }
                }
            }
        })


        $("#commitPermiss").click(function () {
            var msgtype = '';
            $("input[name='m_ch']").each(function (i) {
                if ($(this).is(":checked")) {
                    msgtype += ($(this).val() + ",");
                }
            })
            msgtype = msgtype.substring(0, msgtype.length - 1);

            site.send({
                url: "manager/updateMode.do",
                //async:false
                data: {
                    id: id,
                    msg_types: $("#msgType").val(),
                    sms_title: $("#setSmsTitle").val(),
                    phone_content: editor1.getContent(),
                    mail_title: $("#setMailTitle").val(),
                    mail_content: editor2.getContent(),
                    send_types: msgtype,
                    msg_title: $("#setLetterTitle").val(),
                    msg_content: editor3.getContent()
                },
                callback: function (data) {
                    var list = JSON.parse(data)
                    if (list.type == '1') {
                        //pjaxGo('set/messagetemplate/message_template.html');
                        Constant.fn.Message("T", list.msg);
                        history.go(-1);
                    } else {
                        Constant.fn.Message("F", list.msg);
                    }
                }
            })
        })
    } else {

        $("#mail").show();
        $("#mailInput").prop('checked', true);
        $("#phone").show();
        $("#smsInput").prop('checked', true)
        $("#letter").show();
        $("#letterInput").prop('checked', true)


        var WinsertData;
        $("#commitPermiss").click(function () {
            var msgtype = '';
            WinsertData = {};
            $("input[name='m_ch']").each(function (i) {
                if ($(this).is(":checked")) {
                    msgtype += ($(this).val() + ",");
                }
            })
            msgtype = msgtype.substring(0, msgtype.length - 1);
            WinsertData.send_types = msgtype;
            WinsertData.msg_types = $("#msgType").val()

            if (Constant.getAurlparam('id')) {
                if ($("#setLetterTitle").val()) {
                    WinsertData.msg_title = '【凯盛】' + $("#setLetterTitle").val();
                }
                if (editor3.getContent()) {
                    WinsertData.msg_content = editor3.getContent() + '【凯盛】';
                }
                if ($("#letterInput").is(':checked') && !$("#setLetterTitle").val()) {
                    Constant.fn.Message("I", "请输入站内信标题");
                    return;
                }
                if ($("#letterInput").is(':checked') && !editor3.getContent()) {
                    Constant.fn.Message("I", "请输入站内信内容");
                    return;
                }


                if ($("#setMailTitle").val()) {
                    WinsertData.mail_title = '【凯盛】' + $("#setMailTitle").val();
                }
                if (editor2.getContent()) {
                    WinsertData.mail_content = editor2.getContent() + '【凯盛】';
                }
                if ($("#mailInput").is(':checked') && !$("#setMailTitle").val()) {
                    Constant.fn.Message("I", "请输入邮件标题");
                    return;
                }
                if ($("#mailInput").is(':checked') && !editor2.getContent()) {
                    Constant.fn.Message("I", "请输入邮件内容");
                    return;
                }

                if ($("#setSmsTitle").val()) {
                    WinsertData.sms_title = '【凯盛】' + $("#setSmsTitle").val();
                }
                if (editor1.getContent()) {
                    WinsertData.phone_content = editor1.getContent() + '【凯盛】';
                }
                if ($("#smsInput").is(':checked') && !$("#setSmsTitle").val()) {
                    Constant.fn.Message("I", "请输入短信标题");
                    return;
                }
                if ($("#smsInput").is(':checked') && !editor1.getContent()) {
                    Constant.fn.Message("I", "请输入短信内容");
                    return;
                }
                insertFun();
                /*if (msgtype == '1' && $("#setMailTitle").val() != '' && editor2.getContent() != '') {
                    WinsertData.mail_title = $("#setMailTitle").val();
                    WinsertData.mail_content = editor2.getContent();
                    insertFun();
                } else if (msgtype == '2' && $("#setSmsTitle").val() != '' && editor1.getContent() != '') {
                    WinsertData.sms_title = $("#setSmsTitle").val();
                    WinsertData.phone_content = editor1.getContent();
                    insertFun();
                }else if (msgtype == '3' && $("#setLetterTitle").val() != '' && editor3.getContent() != '') {
                    WinsertData.msg_title = $("#setLetterTitle").val();
                    WinsertData.msg_content = editor3.getContent();
                    insertFun();
                } else if (msgtype == '1,2' && $("#setMailTitle").val() !== '' && $("#setSmsTitle").val() != '' && editor1.getContent() != '' && editor2.getContent() != '') {
                    WinsertData.mail_title = $("#setMailTitle").val();
                    WinsertData.mail_content = editor2.getContent();
                    WinsertData.sms_title = $("#setSmsTitle").val();
                    WinsertData.phone_content = editor1.getContent();
                    insertFun();
                } else {
                    Constant.fn.Message('F', '不能为空')
                }*/
            }
            else {
                if ($("#setLetterTitle").val()) {
                    WinsertData.msg_title = '【凯盛】' + $("#setLetterTitle").val();
                }
                if (editor3.getContent()) {
                    WinsertData.msg_content = editor3.getContent() + '【凯盛】';
                }
                if ($("#letterInput").is(':checked') && !$("#setLetterTitle").val()) {
                    Constant.fn.Message("I", "请输入站内信标题");
                    return;
                }
                if ($("#letterInput").is(':checked') && !editor3.getContent()) {
                    Constant.fn.Message("I", "请输入站内信内容");
                    return;
                }


                if ($("#setMailTitle").val()) {
                    WinsertData.mail_title = '【凯盛】' + $("#setMailTitle").val();
                }
                if (editor2.getContent()) {
                    WinsertData.mail_content = editor2.getContent() + '【凯盛】';
                }
                if ($("#mailInput").is(':checked') && !$("#setMailTitle").val()) {
                    Constant.fn.Message("I", "请输入邮件标题");
                    return;
                }
                if ($("#mailInput").is(':checked') && !editor2.getContent()) {
                    Constant.fn.Message("I", "请输入邮件内容");
                    return;
                }

                if ($("#setSmsTitle").val()) {
                    WinsertData.sms_title = '【凯盛】' + $("#setSmsTitle").val();
                }
                if (editor1.getContent()) {
                    WinsertData.phone_content = editor1.getContent() + '【凯盛】';
                }
                if ($("#smsInput").is(':checked') && !$("#setSmsTitle").val()) {
                    Constant.fn.Message("I", "请输入短信标题");
                    return;
                }
                if ($("#smsInput").is(':checked') && !editor1.getContent()) {
                    Constant.fn.Message("I", "请输入短信内容");
                    return;
                }
                insertFun();
                /*if (msgtype == '1' && $("#setMailTitle").val() != '' && editor2.getContent() != '') {
                    WinsertData.mail_title = '【凯盛】' + $("#setMailTitle").val();
                    WinsertData.mail_content = editor2.getContent() + '【凯盛】';
                    insertFun();
                } else if (msgtype == '2' && $("#setSmsTitle").val() != '' && editor1.getContent() != '') {
                    WinsertData.sms_title = '【凯盛】' + $("#setSmsTitle").val();
                    WinsertData.phone_content = editor1.getContent() + '【凯盛】';
                    insertFun();
                } else if (msgtype == '3' && $("#setLetterTitle").val() != '' && editor3.getContent() != '') {
                    WinsertData.msg_title = '【凯盛】' + $("#setLetterTitle").val();
                    WinsertData.msg_content = editor3.getContent() + '【凯盛】';
                    insertFun();
                }
                else if (msgtype == '1,2' && $("#setMailTitle").val() !== '' && $("#setSmsTitle").val() != '' && editor1.getContent() != '' && editor2.getContent() != '') {
                    WinsertData.mail_title = '【凯盛】' + $("#setMailTitle").val();
                    WinsertData.mail_content = editor2.getContent() + '【凯盛】';
                    WinsertData.sms_title = '【凯盛】' + $("#setSmsTitle").val();
                    WinsertData.phone_content = editor1.getContent() + '【凯盛】';
                    insertFun();
                }*/
            }
        })

    }
    $("#mailInput").click(function () {
        if ($(this).is(":checked")) {
            $("#mail").show();
        } else {
            $("#mail").hide();

        }
    })
    $("#smsInput").click(function () {
        if ($(this).is(":checked")) {
            $("#phone").show();
        } else {
            $("#phone").hide();
        }
    })

    $("#letterInput").click(function () {
        if ($(this).is(":checked")) {
            $("#letter").show();
        } else {
            $("#letter").hide();
        }
    })

    function insertFun() {
        site.send({
            url: "manager/insertTemplate.do",
            //async:false
            data: WinsertData,
            callback: function (data) {
                var list = JSON.parse(data)
                if (list.type == '1') {
                    Constant.fn.Message("T", list.msg);
                    history.go(-1);
                } else {
                    Constant.fn.Message("F", list.msg);
                }
            }
        })
    }
})

